with Ada.Unchecked_Conversion;
with GNAT.Byte_Swapping;
package body BE_Interfaces is

   subtype SEA2 is Stream_Element_Array(1..2); 

   subtype SEA4 is Stream_Element_Array(1..4);

   subtype SEA8 is Stream_Element_Array(1..8);

   function To_SEA2 is new Ada.Unchecked_Conversion(Uint16_be, SEA2);

   function To_Uint16 is new Ada.Unchecked_Conversion(SEA2, Uint16_be);

   function To_SEA2 is new Ada.Unchecked_Conversion(Sint16_be, SEA2);

   function To_Sint16 is new Ada.Unchecked_Conversion(SEA2, Sint16_be);

   function To_SEA4 is new Ada.Unchecked_Conversion(Sint32_be, SEA4);

   function To_Sint32 is new Ada.Unchecked_Conversion(SEA4, Sint32_be);

   function To_SEA4 is new Ada.Unchecked_Conversion(Uint32_be, SEA4);

   function To_Uint32 is new Ada.Unchecked_Conversion(SEA4, Uint32_be);

   function To_SEA8 is new Ada.Unchecked_Conversion(Sint64_be, SEA8);

   function To_Sint64 is new Ada.Unchecked_Conversion(SEA8, Sint64_be);

   function To_SEA8 is new Ada.Unchecked_Conversion(Uint64_be, SEA8);

   function To_Uint64 is new Ada.Unchecked_Conversion(SEA8, Uint64_be);

   ------------------------------------------------------------------

   function Input (
      From : not null access Root_Stream_Type'Class
      ) return Sint16_be
   is
      N : SEA2;
   begin
      SEA2'Read(From, N);
      GNAT.Byte_Swapping.Swap2(N'Address);
      return To_Sint16(N);
   end Input;

   procedure Output (
      To   : not null access Root_Stream_Type'Class;
      From : Sint16_be 
      )
   is
      N : SEA2 := To_SEA2(From);
   begin
      GNAT.Byte_Swapping.Swap2(N'Address);
      SEA2'Write(To, N);
   end Output;

   procedure Read (
      From : not null access Root_Stream_Type'Class;
      To   : out Sint16_be
      )
   is
      N : SEA2;
   begin
      SEA2'Read(From, N);
      GNAT.Byte_Swapping.Swap2(N'Address);
      To := To_Sint16(N);
   end Read;

   function Input (
      From : not null access Root_Stream_Type'Class
      ) return Uint16_be
   is
      N : SEA2;
   begin
      SEA2'Read(From, N);
      GNAT.Byte_Swapping.Swap2(N'Address);
      return To_Uint16(N);
   end Input;

   procedure Output (
      To   : not null access Root_Stream_Type'Class;
      From : Uint16_be 
      )
   is
      N : SEA2 := To_SEA2(From);
   begin
      GNAT.Byte_Swapping.Swap2(N'Address);
      SEA2'Write(To, N);
   end Output;

   procedure Read (
      From : not null access Root_Stream_Type'Class;
      To   : out Uint16_be
      )
   is
      N : SEA2;
   begin
      SEA2'Read(From, N);
      GNAT.Byte_Swapping.Swap2(N'Address);
      To := To_Uint16(N);
   end Read;


   -----------------------------------------------------------


   function Input (
      From : not null access Root_Stream_Type'Class
      ) return Sint32_be
   is
      N : SEA4;
   begin
      SEA4'Read(From, N);
      GNAT.Byte_Swapping.Swap4(N'Address);
      return To_Sint32(N);
   end Input;

   procedure Output (
      To   : not null access Root_Stream_Type'Class;
      From : Sint32_be 
      )
   is
      N : SEA4 := To_SEA4(From);
   begin
      GNAT.Byte_Swapping.Swap4(N'Address);
      SEA4'Write(To, N);
   end Output;

   procedure Read (
      From : not null access Root_Stream_Type'Class;
      To   : out Sint32_be
      )
   is
      N : SEA4;
   begin
      SEA4'Read(From, N);
      GNAT.Byte_Swapping.Swap4(N'Address);
      To := To_Sint32(N);
   end Read;

   function Input (
      From : not null access Root_Stream_Type'Class
      ) return Uint32_be
   is
      N : SEA4;
   begin
      SEA4'Read(From, N);
      GNAT.Byte_Swapping.Swap4(N'Address);
      return To_Uint32(N);
   end Input;

   procedure Output (
      To   : not null access Root_Stream_Type'Class;
      From : Uint32_be 
      )
   is
      N : SEA4 := To_SEA4(From);
   begin
      GNAT.Byte_Swapping.Swap4(N'Address);
      SEA4'Write(To, N);
   end Output;

   procedure Read (
      From : not null access Root_Stream_Type'Class;
      To   : out Uint32_be
      )
   is
      N : SEA4;
   begin
      SEA4'Read(From, N);
      GNAT.Byte_Swapping.Swap4(N'Address);
      To := To_Uint32(N);
   end Read;

   -----------------------------------------------------------


   function Input (
      From : not null access Root_Stream_Type'Class
      ) return Sint64_be
   is
      N : SEA8;
   begin
      SEA8'Read(From, N);
      GNAT.Byte_Swapping.Swap4(N'Address);
      return To_Sint64(N);
   end Input;

   procedure Output (
      To   : not null access Root_Stream_Type'Class;
      From : Sint64_be 
      )
   is
      N : SEA8 := To_SEA8(From);
   begin
      GNAT.Byte_Swapping.Swap4(N'Address);
      SEA8'Write(To, N);
   end Output;

   procedure Read (
      From : not null access Root_Stream_Type'Class;
      To   : out Sint64_be
      )
   is
      N : SEA8;
   begin
      SEA8'Read(From, N);
      GNAT.Byte_Swapping.Swap4(N'Address);
      To := To_Sint64(N);
   end Read;

   function Input (
      From : not null access Root_Stream_Type'Class
      ) return Uint64_be
   is
      N : SEA8;
   begin
      SEA8'Read(From, N);
      GNAT.Byte_Swapping.Swap4(N'Address);
      return To_Uint64(N);
   end Input;

   procedure Output (
      To   : not null access Root_Stream_Type'Class;
      From : Uint64_be 
      )
   is
      N : SEA8 := To_SEA8(From);
   begin
      GNAT.Byte_Swapping.Swap4(N'Address);
      SEA8'Write(To, N);
   end Output;

   procedure Read (
      From : not null access Root_Stream_Type'Class;
      To   : out Uint64_be
      )
   is
      N : SEA8;
   begin
      SEA8'Read(From, N);
      GNAT.Byte_Swapping.Swap4(N'Address);
      To := To_Uint64(N);
   end Read;

end BE_Interfaces;
